package com.gy.yemall.yemallware.service.impl;

import com.gy.common.constant.WareConstant;
import com.gy.yemall.yemallware.entity.PurchaseDetailEntity;
import com.gy.yemall.yemallware.entity.vo.CompleteBuyVo;
import com.gy.yemall.yemallware.entity.vo.ItemsVo;
import com.gy.yemall.yemallware.entity.vo.MergeVo;
import com.gy.yemall.yemallware.service.PurchaseDetailService;
import com.gy.yemall.yemallware.service.WareSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gy.common.utils.PageUtils;
import com.gy.common.utils.Query;

import com.gy.yemall.yemallware.dao.PurchaseDao;
import com.gy.yemall.yemallware.entity.PurchaseEntity;
import com.gy.yemall.yemallware.service.PurchaseService;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Autowired
    private WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryUnreceive(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status", 0)
                        .or().eq("status",1)
        );

        return new PageUtils(page);
    }

    // TODO
    @Override
    public void merge(MergeVo mergeVo) {
        Long purchaseId = mergeVo.getPurchaseId();
        if (purchaseId == null) {
            // 需要新建一个采购单
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setStatus(WareConstant.PurchaseStatus.CREATE_STATUS.getCode());
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();
        }
        // 进行合并
        List<Long> items = mergeVo.getItems();
        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> collect = items.stream().map((item) -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            // 更新彩虹需求表需要设置id
            purchaseDetailEntity.setId(item);
            // 将采购需求表和采购订单表进行绑定
            purchaseDetailEntity.setPurchaseId(finalPurchaseId);
            purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatus.ASSGINED_STATUS.getCode());
            return purchaseDetailEntity;
        }).collect(Collectors.toList());

        purchaseDetailService.updateBatchById(collect);

        // 每次都更新时间采购订单
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseId);
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);
    }

    @Override
    public void receive(List<Long> ids) {
        List<PurchaseEntity> collect = ids.stream().map((id) -> {
            PurchaseEntity byId = this.getById(id);
            return byId;
        }).filter((item) -> {
            // 采购订单状态为0或1才可以进行领取
            if (item.getStatus() == WareConstant.PurchaseStatus.CREATE_STATUS.getCode() ||
                    item.getStatus() == WareConstant.PurchaseStatus.ASSGINED_STATUS.getCode()) {
                return true;
            }
            return false;
        }).map((item) -> {
            // 进行领取即状态的修改
            item.setStatus(WareConstant.PurchaseStatus.BUYING_STATUS.getCode());
            item.setUpdateTime(new Date());
            return item;
        }).collect(Collectors.toList());

        // 进行更新
        this.updateBatchById(collect);

        // 更新采购需求表通过采购订单id
        collect.forEach((item) -> {
            List<PurchaseDetailEntity> list = purchaseDetailService.queryEntityByPurchaseId(item.getId());
            List<PurchaseDetailEntity> collect1 = list.stream().map((entity) -> {
                PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
                purchaseDetailEntity.setId(entity.getId());
                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatus.BUYING_STATUS.getCode());
                return purchaseDetailEntity;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(collect1);
        });
    }

    @Override
    public void completeBuy(CompleteBuyVo completeBuyVo) {
        // 更新采购项
        Boolean flag = true;
        // 将所有符合条件的加入到list集合进行更新
        List<PurchaseDetailEntity> list = new ArrayList<>();
        List<ItemsVo> items = completeBuyVo.getItems();
        for (ItemsVo item : items) {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            if (item.getStatus() == WareConstant.PurchaseDetailStatus.BUYINGERROR_STATUS.getCode()) {
                flag = false;
                purchaseDetailEntity.setStatus(item.getStatus());
            } else {
                // 说明该采购项完成了
                purchaseDetailEntity.setStatus(item.getStatus());
                // 更新库存
                PurchaseDetailEntity entity = purchaseDetailService.getById(item.getItemId());
                wareSkuService.addStock(entity.getSkuId(),entity.getWareId(),entity.getSkuNum());
            }
            purchaseDetailEntity.setId(item.getItemId());
            list.add(purchaseDetailEntity);
        }
        purchaseDetailService.updateBatchById(list);

        // 采购单id
        Long purchaseId = completeBuyVo.getId();

        // 更新采购订单,采购项都完成才能进行更新状态
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseId);
        purchaseEntity.setUpdateTime(new Date());
        purchaseEntity.setStatus(flag ? WareConstant.PurchaseStatus.HASCOMPLETE_STATUS.getCode()
                : WareConstant.PurchaseStatus.BUYINGERROR_STATUS.getCode());
        this.updateById(purchaseEntity);
    }

}